<?php


define ('THIS_SCRIPT', 'assistfb/facebooktest.php');

require_once('../lib-common.php');


require_once('facebooktest.inc');

$pi_name="assistfb";


$appid = $_ASSISTFB_CONF['facebook_app_Id'];
$secret=$_ASSISTFB_CONF['facebook_app_secret'];
	
//$appid = $_CONF['facebook_consumer_key'];
//$secret= $_CONF['facebook_consumer_secret'];

echo "appid=".$appid."<br>";
echo "secret=".$secret."<br>";

$page_title="facebook api test";
$display = COM_siteHeader('menu', $page_title);

//$user=assistfb_getUserinfo($appid,$secret	);
//echo "user".$user;

$ary=assistfb_getAppProperties($appid,$secret);
echo "ary<br>";
foreach( $ary as $nm => $value ){
	echo $nm."=".$value."<br>";
}

//$item="user_id";
//assistfb_getfacebookinfo($item);
//echo $ditem."=".assistfb_getfacebookinfo($item)."<br>";

//$item="email";
//assistfb_getfacebookinfo($item);
//echo $item."=".assistfb_getfacebookinfo($item)."<br>";

//$item="location_name";
//assistfb_getfacebookinfo($item);
//echo $item."=".assistfb_getfacebookinfo($item)."<br>";

//$item="language_name";
//assistfb_getfacebookinfo($item);
//echo $item."=".assistfb_getfacebookinfo($item)."<br>";

//	$dummy=assistfb_getAppProperties();

function assistfb_getUserinfo(
	$appid
	,$secret
) {
	global $_CONF;
	$facebook_conf = array();
	$facebook_conf['appId'] = $appid;
	$facebook_conf['secret'] = $secret;
	$facebook_conf['fileUpload'] = false; // optional
	
	echo "appid2:".$facebook_conf['appId']."<br>";
	echo "secret2:".$facebook_conf['secret']."<br>";
	
	$facebook = new Facebook($facebook_conf);
	
	if ($user==""){
		$user = $facebook->getUser();
		echo "user2=".$user."<br>";
	}
	
	if ($user) {
		try {
			$user_profile = $facebook->api('/me');
			//fncdisplayuserprofile($user_profile);
			
		} catch (FacebookApiException $e) {
			error_log($e);
			$user = null;
		}
	}
	
	$return=$user;
	
	return $return ;
}

function assistfb_getAppProperties(
	$appid
	,$secret
) {
	global $_CONF;

	
	$facebook_conf = array();
	$facebook_conf['appId'] = $appid;
	$facebook_conf['secret'] = $secret;
	$facebook_conf['fileUpload'] = false; // optional
	
	$page_id="316013445091954";//@@@@@
	
	//echo "appid:".$facebook_conf['appId']."<br>";
	//echo "secret:".$facebook_conf['secret']."<br>";
	
	
	$facebook = new Facebook($facebook_conf);
	
	if ($user<>""){
		$user = $facebook->getUser();
	}
	if ($user) {
		try {
			$user_profile = $facebook->api('/me');
			//fncdisplayuserprofile($user_profile);
		} catch (FacebookApiException $e) {
			error_log($e);
			$user = null;
		}
	}
	
	$access_token = $facebook->getAccessToken();
	echo "access_token=".$access_token ."<br>";
	
	
	$arg = array();
	$arg['method'] = 'admin.getAppProperties';
	
	$pp ="about_url";
	$pp.=",app_id";
	$pp.=",authorize_url";
	$pp.=",base_domain";
	$pp.=",base_domains";
	
	$pp.=",callback_url";
	$pp.=",canvas_name";
	$pp.=",connect_logo_url";
	
	
	$pp.=",connect_preview_template";
	$pp.=",connect_reclaim_url";
	$pp.=",connect_url";
	$pp.=",contact_email";
	$pp.=",creator_uid";
	$pp.=",dashboard_url";
	$pp.=",default_column";
	$pp.=",description";
	
	
	$pp.=",desktop";
	$pp.=",dev_mode";
	$pp.=",edit_url";
	$pp.=",email";
	$pp.=",help_url";
	$pp.=",icon_url";
	$pp.=",iframe_enable_util";
	$pp.=",ignore_ip_whitelist_for_ss";
	$pp.=",info_changed_url";
	$pp.=",installable";
	$pp.=",ip_list";
	
	$pp.=",is_mobile";
	$pp.=",locale";
	$pp.=",logo_url";
	$pp.=",message_action";
	$pp.=",post_authorize_redirect_url";
	$pp.=",preload_fql";
	$pp.=",preload_fql_multiquery";
	$pp.=",privacy_url";
	
	$pp.=",private_install";
	$pp.=",profile_tab_url";
	$pp.=",publish_action";
	$pp.=",publish_self_action";
	$pp.=",publish_self_url";
	$pp.=",publish_url";
	
	//$pp.=",quick_transitions";
	$pp.=",secure_callback_url";
	$pp.=",secure_page_tab_url";
	
	$pp.=",support_url";
	$pp.=",tab_default_name";
	$pp.=",targeted";
	$pp.=",tos_url";
	$pp.=",uninstall_url";
	$pp.=",use_iframe";
	$pp.=",video_rentals";
	
	$arg['properties'] = $pp;
	
	try { 
		$api = $facebook->api($arg);
		//echo "api=".$api."<br>";
		$w=rtrim(ltrim($api,"{"),"}");
		$apiary=explode(',',$w);
		$ary=array();
		foreach( $apiary as $nm => $value ){
			$wary=explode(':',$value);
			$ary[ltrim(rtrim($wary[0],'"'),'"')]=ltrim(rtrim($wary[1],'"'),'"');
		}
		$return=$ary;
	} catch (FacebookApiException $e) {
		error_log($e);
		echo "err=".$e."<br>";
		$return=$e;
	}	
	
	

	return $return;
	
}


function assistfb_getfacebookinfo(
	$item
	,$user=""
	,$page_id=""
) {
	global $_CONF;
	global $_DATABOX_CONF;
	

	$return;
	
	$facebook_conf = array();
	$facebook_conf['appId'] = $_ASSISTFB_CONF['facebook_app_Id'];
	$facebook_conf['secret'] = $_ASSISTFB_CONF['facebook_app_secret	'];
	$facebook_conf['fileUpload'] = false; // optional
	
	$facebook_conf['appId'] = $_CONF['facebook_consumer_key'];
	$facebook_conf['secret'] = $_CONF['facebook_consumer_secret'];
	
	$page_id="316013445091954";//@@@@@
	
	echo "appid:".$facebook_conf['appId']."<br>";
	echo "secret:".$facebook_conf['secret']."<br>";
	
	echo "user1:".$user."<br>";
	
	$facebook = new Facebook($facebook_conf);
	
	
	echo "user2:".$user."<br>";
	
	if ($user<>""){
		$user = $facebook->getUser();
	}
	echo "user3:".$user."<br>";
	
	if ($user) {
		try {
			$user_profile = $facebook->api('/me');
			//fncdisplayuserprofile($user_profile);
		} catch (FacebookApiException $e) {
			error_log($e);
			$user = null;
		}
	}
	
	$access_token = $facebook->getAccessToken();
	echo "access_token=".$access_token ."<br>";
	$secret = $facebook->getApiSecret();
	echo "secret=".$secret ."<br>";
	$appId = $facebook->getAppId();
	echo "appId=".$appId ."<br>";
	
			echo "<br>-----auth.getSession<br>";
			$arg = array();
			$arg['method'] = 'auth.getSession';
			$arg['access_token'] = $access_token;
			try {
				$api = $facebook->api($arg);
			} catch (FacebookApiException $e) {
				error_log($e);
				echo "err=".$e."<br>";
				$api = "err";
			}	
	
	
			echo "<br>-----pages.isadmin<br>";
			$arg = array();
			$arg['method'] = 'pages.isadmin';
			$arg['page_id'] = $page_id;
			$arg['uid'] = 	$user;
			$arg['access_token'] = 	$user;
			try {
				$api = $facebook->api($arg);
			} catch (FacebookApiException $e) {
				error_log($e);
				echo "err=".$e."<br>";
				$api = "err";
			}	
	echo "api=".$api."<br>";
	
	
	switch( $item ) {
        case 'user_id':
			$return=$user_profile['location']['id'];
			break;
		case 'name':
		case 'first_name':
		case 'last_name':
		case 'link':
		case 'username':
		case 'gender':
		case 'email':
		case 'timezone':
		case 'locale':
		case 'verified':
		case 'updated_time':
			$return=$user_profile[$item];
			break;
		
		case 'location_id':
			$return=$user_profile['location']['id'];
		case 'location_name':
			$return=$user_profile['location']['name'];
			break;
		case 'language_id':
			$return=$user_profile['languages'][0]['id	'];
			break;
		case 'language_name':
			$return=$user_profile['languages'][0]['name'];
			break;
		
		case 'isPageAdmin':
			break;
		
		
		
	}

	return $return;
	
}
	//
function assistfb_facebook_test (
) {
	global $_CONF;
	global $_DATABOX_CONF;
	//$_DATABOX_CONF['facebook_lib']=$_CONF['path'] . 'facebook/';;

	$facebook_conf = array();
	$facebook_conf['appId'] = $_ASSISTFB_CONF['facebook_app_Id'];
	$facebook_conf['secret'] = $_ASSISTFB_CONF['facebook_app_secret	'];
	$facebook_conf['fileUpload'] = false; // optional
	
	$facebook_conf['appId'] = $_CONF['facebook_consumer_key'];
	$facebook_conf['secret'] = $_CONF['facebook_consumer_secret'];
	
	//echo "appid:".$facebook_conf['appId']."<br>";
	//echo "secret:".$facebook_conf['secret']."<br>";
	
	
	$facebook = new Facebook($facebook_conf);
	
	
	
	// Get User ID
	$user = $facebook->getUser();
	
	echo "user=".$user."<br>";
	
	if ($user) {
		try {
			// Proceed knowing you have a logged in user who's authenticated.
			$user_profile = $facebook->api('/me');
//			echo "user_profule=".$user_profile."<br>";
//			$dummy=fncdisplayuserprofile($user_profile);
			
		} catch (FacebookApiException $e) {
			error_log($e);
			$user = null;
			echo "user2=".$user."<br>";
		}
	}
	
//	echo "user3=".$user."<br>";
	// Login or logout url will be needed depending on current user state.
	if ($user) {
		$logoutUrl = $facebook->getLogoutUrl();
		echo "logoutUrl=".$logoutUrl."<br>";
		$w="<a href=\"{$logoutUrl}\">Logout</a>";
		echo $w;
		
	} else {
        $loginUrl = $facebook->getLoginUrl();
		echo "loginUrl=".$loginUrl."<br>";
		$w="<a href=\"{$loginUrl}\">Login with Facebook</a>";
		echo $w;
	}
	
//	echo "-----SESSION<br>";
//	print_r($_SESSION);
//	echo "-----user_profile<br>";
//	print_r($user_profile);
	
	//
//	$naitik = $facebook->api('/naitik');
//	echo "<br>-----naitik_name<br>";
//    echo $naitik['name']."<br>";
//	echo '<img src="https://graph.facebook.com/naitik/picture">';
//    echo $naitik['name'];
	
	//
//	echo "<br>-----userinfo<br>";
//	$username=$user_profile['username'];
//	$userinfo = $facebook->api('/'.$username);
//    echo $userinfo['name']."<br>";
//	echo '<img src="https://graph.facebook.com/'.$username.'/picture">';
//    echo $userinfo['name'];
	
	//
//	echo "<br>-----userinfo<br>";
//	$username=$user_profile['username'];
//	$userinfo = $facebook->api('/'.$username);
//    echo $userinfo['name']."<br>";
//	echo '<img src="https://graph.facebook.com/'.$username.'/picture">';
//    echo $userinfo['name'];
	
	
	
//	echo "<br>-----user_profile<br>";
//	if ($user) {
//		try {
			// Proceed knowing you have a logged in user who's authenticated.
//			$user_profile = $facebook->api('/me');
//			$dummy=fncdisplayuserprofile($user_profile);
			
//		} catch (FacebookApiException $e) {
//			error_log($e);
//			$user = null;
//		}
//	}
	
	
	//------------------------------------
    //public function getResult() {
//	echo "<br>-----result<br>";
//	$result = $facebook->getResult();
//	echo $result;
//Fatal error: Call to undefined method Facebook::getResult() in /virtual/tsuchi/public_html/labo3.itsup.net/gl181/databox/facebooktest.php on line 126
	
	//public function getType() {
//	echo "<br>-----type<br>";
//	$type = $facebook->getType();
//	echo $type;
	
if (1==0){
	//public function getAppId() {
	echo "<br>-----appid<br>";
	$appid = $facebook->getAppId();
	echo $appid;
	
	//public function getApiSecret() {
	echo "<br>-----ApiSecret<br>";
	$appSecret = $facebook->getApiSecret();
	echo $appSecret;
	
    //public function getAccessToken() {
	echo "<br>-----AccessToken<br>";
	$AccessToken = $facebook->getAccessToken();
	echo $AccessToken;
	
	//public function getSignedRequest() {
	echo "<br>-----SignedRequest<br>";
	$AccessToken = $facebook->getSignedRequest();
	echo $SignedRequest;
	
	//public function getUser() {
	echo "<br>-----User<br>";
	$wUser = $facebook->getUser();
	echo $wUser;
	
    //public function getLoginUrl($params=array()) {
	//public function getLogoutUrl($params=array()) {
	
    //public function getLoginStatusUrl($params=array()) {
	echo "<br>-----LoginStatusUrl<br>";
	$wUser = $facebook->getLoginStatusUrl();
	echo $LoginStatusUrl;
		
	//public function api(/* polymorphic */) {
	
	echo "<br>-----api<br>";
	$api = $facebook->api('/me','GET');
    echo "api=".$api."<br>";
}		
	
	echo "<br>-----fql<br>";
	$arg = array();
	$arg['method'] = 'fql.query';
	$arg['query']= 'SELECT name,uid,languages from user where uid = ' . $user;
    $api = $facebook->api($arg);
	echo "api=".$api."<br>";
	$dummy=fncdisplayuserprofile($api);
	
	echo "<br>-----fql<br>";
	$arg = array();
	$arg['method'] = 'fql.query';
	//$arg['query']= 'SELECT page_id, type from page_admin WHERE uid= ' . $user;
	//$arg['query']= 'SELECT page_id from page_admin WHERE page_id=125427737531030 AND uid= ' . $user;
	$arg['query']= 'SELECT page_id, pic FROM page WHERE username = "ivywecojp"';
	
	$api = $facebook->api($arg);
	echo "api=".$api."<br>";
	//print_r($api);
	$dummy=fncdisplayuserprofile($api);
	
	echo "<br>-----pages.isadmin<br>";
	$arg = array();
	$arg['method'] = 'pages.isadmin';
    //$api = $facebook->api($arg);
	//echo "api=".$api."<br>";
	
}


function fncdisplayuserprofile (
	$user_profile
) {
	$rt=array();
	if (is_array($user_profile)){
		foreach( $user_profile as $nm => $value ){
			if (is_array($user_profile[$nm])){
				$wkary2=$user_profile[$nm];
				foreach( $wkary2 as $nm2 => $value2 ){
					if (is_array($wkary2[$nm2])){
						$wkary3=$wkary2[$nm2];
						foreach( $wkary3 as $nm3 => $value3 ){
							if  (is_array($wkary3[$nm3])){
								$wkary4=$wkary3[$nm3];
								foreach( $wkary4 as $nm4 => $value4 ){
									echo "A=".$nm."=".$nm2."=".$nm3."=".$nm4."=".$value4."<br>";
									$rt[$nm4]=$value4;
								}
							}else{	
								echo "B=".$nm."=".$nm2."=".$nm3."=".$value3."<br>";
								$rt[$nm3]=$value3;
							}
						}
					}else{	
						echo "C=".$nm."=".$nm2."=".$value2."<br>";
						$rt[$nm2]=$value2;
					}
				}
			}else{	
				echo $nm."=".$value."<br>";
				$rt[$nm]=$value;
			}
		}
    }
	return $rt;
}

?>